package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.OneSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.ThreeSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.TwoSifeSecondActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.WebViewActivity;
import com.wangtao.firstapp.modles.SifeData;

import java.util.List;

/**
 * 适配精选页面的适配器(需复用，同一个ViewPager中的所有Fragment都复用此适配器)
 * Created by lanouhn on 16/8/25.
 */
public class RVSifeAdapter extends RecyclerView.Adapter<RVSifeAdapter.ListHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    private Context context;
    private List<SifeData.DataBean.TopicBean> sifeData;

    public RVSifeAdapter(Context context, List<SifeData.DataBean.TopicBean> sifeData) {
        this.context = context;
        this.sifeData = sifeData;
    }



    @Override
    public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_sift_fragment, null);
        ListHolder holder = new ListHolder(view);
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();
        return holder;
    }


    @Override
    public void onBindViewHolder(final ListHolder holder, final int position) {

        //SifeData.DataBean dataBean = sifeData.getData();

        Picasso.with(context).load(sifeData.get(position).getPic()).into(holder.pic);
        holder.title.setText(sifeData.get(position).getTitle());
        holder.nickname.setText(sifeData.get(position).getUser().getNickname());
        
        if (sifeData.get(position).getLikes().equals("")) {
            //getIs_official() == 1 && getType().equals("") 层级的Likes
            holder.likes.setText("0");
            //getIs_official() == 1 && !getType().equals("") 层级的Likes
            holder.views_love.setText("0");
        } else {
            //getIs_official() == 1 && getType().equals("") 层级的Likes
            holder.likes.setText(sifeData.get(position).getLikes());
            //getIs_official() == 1 && !getType().equals("") 层级的Likes
            holder.views_love.setText(sifeData.get(position).getLikes());
        }


        if (sifeData.get(position).getComments().equals("")) {
            //getIs_official() == 1 && getType().equals("") 层级的Comments
            holder.comments.setText("0");
            //getIs_official() == 0 层级的Comments
            holder.looks_comments.setText("0");
        } else {
            //getIs_official() == 1 && getType().equals("") 层级的Comments
            holder.comments.setText(sifeData.get(position).getComments());
            //getIs_official() == 0 层级的Comments
            holder.looks_comments.setText(sifeData.get(position).getComments());
        }


        if (sifeData.get(position).getViews().equals("")) {
            //getIs_official() == 0 层级的Views
            holder.lookes.setText("0");
            //getIs_official() == 1 && !getType().equals("") 层级的Views
            holder.view_loves.setText("0");
        } else {
            //getIs_official() == 0 层级的Views
            holder.lookes.setText(sifeData.get(position).getViews());
            //getIs_official() == 1 && !getType().equals("") 层级的Views
            holder.view_loves.setText(sifeData.get(position).getViews());
        }

        //原创文章,有图
        if (sifeData.get(position).
                getType().equals("2")&&sifeData.get(position).
                getType_id().equals("1")) {
            holder.look.setVisibility(View.VISIBLE);
            holder.lookes.setVisibility(View.VISIBLE);
            holder.look_comment.setVisibility(View.VISIBLE);
            holder.looks_comments.setVisibility(View.VISIBLE);

            holder.like.setVisibility(View.INVISIBLE);
            holder.likes.setVisibility(View.INVISIBLE);
            holder.comment.setVisibility(View.INVISIBLE);
            holder.comments.setVisibility(View.INVISIBLE);

            holder.view_love.setVisibility(View.INVISIBLE);
            holder.view_loves.setVisibility(View.INVISIBLE);
            holder.love.setVisibility(View.INVISIBLE);
            holder.views_love.setVisibility(View.INVISIBLE);

        }

        //小糖君，有图
        if (sifeData.get(position).
                getType().equals("")&&sifeData.get(position).
                getType_id().equals("")) {
            holder.like.setVisibility(View.VISIBLE);
            holder.likes.setVisibility(View.VISIBLE);
            holder.comment.setVisibility(View.VISIBLE);
            holder.comments.setVisibility(View.VISIBLE);

            holder.view_love.setVisibility(View.INVISIBLE);
            holder.view_loves.setVisibility(View.INVISIBLE);
            holder.love.setVisibility(View.INVISIBLE);
            holder.views_love.setVisibility(View.INVISIBLE);

            holder.look.setVisibility(View.INVISIBLE);
            holder.lookes.setVisibility(View.INVISIBLE);
            holder.look_comment.setVisibility(View.INVISIBLE);
            holder.looks_comments.setVisibility(View.INVISIBLE);

        }
        //小唐君，无图
        if (sifeData.get(position).
                getType().equals("5")&&sifeData.get(position).
                getType_id().equals("")) {
            holder.view_love.setVisibility(View.VISIBLE);
            holder.view_loves.setVisibility(View.VISIBLE);
            holder.love.setVisibility(View.VISIBLE);
            holder.views_love.setVisibility(View.VISIBLE);

            holder.like.setVisibility(View.INVISIBLE);
            holder.likes.setVisibility(View.INVISIBLE);
            holder.comment.setVisibility(View.INVISIBLE);
            holder.comments.setVisibility(View.INVISIBLE);

            holder.look.setVisibility(View.INVISIBLE);
            holder.lookes.setVisibility(View.INVISIBLE);
            holder.look_comment.setVisibility(View.INVISIBLE);
            holder.looks_comments.setVisibility(View.INVISIBLE);
        }

        //WebView
        if (sifeData.get(position).
                getType().equals("2")&&sifeData.get(position).
                getType_id().equals("")) {
            holder.look.setVisibility(View.VISIBLE);
            holder.lookes.setVisibility(View.VISIBLE);
            holder.look_comment.setVisibility(View.VISIBLE);
            holder.looks_comments.setVisibility(View.VISIBLE);

            holder.like.setVisibility(View.INVISIBLE);
            holder.likes.setVisibility(View.INVISIBLE);
            holder.comment.setVisibility(View.INVISIBLE);
            holder.comments.setVisibility(View.INVISIBLE);

            holder.view_love.setVisibility(View.INVISIBLE);
            holder.view_loves.setVisibility(View.INVISIBLE);
            holder.love.setVisibility(View.INVISIBLE);
            holder.views_love.setVisibility(View.INVISIBLE);

        }

        //跳转至，作者介绍页面
        holder.nickname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID=sifeData.get(position).getUser().getUser_id();
                Intent intent=new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
                //Toast.makeText(context, "作者信息界面", Toast.LENGTH_SHORT).show();
            }
        });


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String item_ID = sifeData.get(position).getId();

//                //文章，图，卖东西
//                String urlSecond = "http://open4.bantangapp.com/topic/newInfo";
//                String paramsSecond = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + item_ID + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";
//
//                //大家都在看
//                String urlLook = "http://open4.bantangapp.com/topics/topic/relationRec";
//                String paramsLook = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + item_ID;
//
//                //评论
//                String urlReview = "http://open4.bantangapp.com/topics/comment/list";
//                String paramsReview = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=0&pagesize=20&id=" + item_ID;

                //原创文章,有图
                if (sifeData.get(position).
                        getType().equals("2")&&sifeData.get(position).
                        getType_id().equals("1")) {
                    Intent intent = new Intent(context, OneSifeSecondActivity.class);
                    intent.putExtra("item_ID",item_ID);
                    context.startActivity(intent);
                }

                //小糖君，有图
                if (sifeData.get(position).
                        getType().equals("")&&sifeData.get(position).
                        getType_id().equals("")) {
                    Intent intent = new Intent(context, TwoSifeSecondActivity.class);
                    intent.putExtra("item_ID",item_ID);
                    context.startActivity(intent);
                }
                //小唐君，无图
                if (sifeData.get(position).
                        getType().equals("5")&&sifeData.get(position).
                        getType_id().equals("")) {
                    Intent intent = new Intent(context, ThreeSifeSecondActivity.class);
                    intent.putExtra("item_ID",item_ID);
                    context.startActivity(intent);
                }

                //WebView
                if(sifeData.get(position).
                        getType().equals("2")&&sifeData.get(position).
                        getType_id().equals("")){
                    Intent intent=new Intent(context, WebViewActivity.class);
                    intent.putExtra("item_ID",item_ID);
                    context.startActivity(intent);
                }

            }
        });

    }


    @Override
    public int getItemCount() {
        return sifeData.size();
    }


    public class ListHolder extends RecyclerView.ViewHolder {

        private TextView title, nickname, likes, comments, lookes, looks_comments, view_loves, views_love;
        private ImageView pic, like, comment, look, look_comment, love, view_love;

        public ListHolder(View itemView) {
            super(itemView);
            pic = (ImageView) itemView.findViewById(R.id.pic_item_sift);
            title = (TextView) itemView.findViewById(R.id.title_item_sift);
            nickname = (TextView) itemView.findViewById(R.id.nickname_item_sift);

            //getIs_official() == 1 && getType().equals("") 层级
            like = (ImageView) itemView.findViewById(R.id.iv_like);
            likes = (TextView) itemView.findViewById(R.id.likes_item_sift);
            comment = (ImageView) itemView.findViewById(R.id.iv_likes_comments);
            comments = (TextView) itemView.findViewById(R.id.comments_likes_item_sift);

            //getIs_official() == 0 层级
            look = (ImageView) itemView.findViewById(R.id.iv_look);
            lookes = (TextView) itemView.findViewById(R.id.looks_item_sift);
            look_comment = (ImageView) itemView.findViewById(R.id.iv_look_comment);
            looks_comments = (TextView) itemView.findViewById(R.id.comments_looks_item_sift);

            //getIs_official() == 1 && !getType().equals("") 层级
            view_love = (ImageView) itemView.findViewById(R.id.iv_view_love);
            view_loves = (TextView) itemView.findViewById(R.id.views_loves_item_sift);
            love = (ImageView) itemView.findViewById(R.id.iv_love);
            views_love = (TextView) itemView.findViewById(R.id.loves_item_sift);

        }

    }
}